'use strict';

const await = require('await-stream-ready/lib/await');

const Controller = require('egg').Controller;

class Rescue_stationController extends Controller {
    async index(){
        const { ctx,app } = this;
    
        let tabs = [{
            name:"全部",
            url:"/admin/station",
            active:false
        },{
            name:"未通过",
            url:"?identified=0",
            identified:0,
            active:false
        },{
            name:"已通过",
            url:"?identified=1",
            identified:1,
            active:false
        }]
    
    
        tabs = tabs.map(item=>{
            if( (!ctx.query.identified && ctx.query.identified != 0 && item.url === '/admin/station') || item.identified == ctx.query.identified){
                item.active = true
            }
            return item
        })
        console.log("aaaa",ctx.query)
    
        let where = (!ctx.query.identified && ctx.query.identified != 0) ? {} : {
            identified: ctx.query.identified
        }
    
        let data = await ctx.page('RescueStation',{},{
            include:[{
                model:app.model.User,
                as:'ori',
                where: where
            }]
        })
    
        data = JSON.parse(JSON.stringify(data))
    
        await ctx.renderTemplate({
            title:"救助站认证表",
            tempType: "table",
            table:{
                tabs,
                // 表头
                columns:[{
                        title:"id",
                        fixed: 'center',
                        width: 50,
                        key:"id"
                    },{
                    title:"机构名称",
                    fixed: 'left',
                    width: 180,
                    render(item) {
                        let image = item.ori.avatar ? item.ori.avatar : '/public/assets/img/profiles/avatar-03.jpg'
                        return `
                        <h2 class="table-avatar">
                             <a href="profile.html" class="avatar avatar-sm mr-2"><img class="avatar-img rounded-circle" src="${image}"></a>
                             <a href="profile.html">${item.ori.username}</a>
                        </h2>
                        `
                    }
                },{
                    title:"简介",
                    fixed: 'center',
                    width: 100,
                    key:"briefIntroduction"
                },{
                    title:"位置",
                    fixed: 'center',
                    width: 150,
                    key:"specificLocation"
                },{
                    title:"联系电话",
                    fixed: 'center',
                    width: 150,
                    key:"officialPhone"
                },{
                    title:"受权状态",
                    fixed: 'center',
                    width: 100,
                    render(item){
                        if(item.ori.identified == 0){
                            let typeName="未受权"
                            return `
                            ${typeName}
                            `
                        }
                        else if(item.ori.identified == 1){
                            let typeName="已受权"
                            return `
                            ${typeName}
                            `
                        }
                    }
                },{
                    title: "操作",
                    width: 200,
                    fixed: 'center',
                    render(item){
                        let check=''
                        if(item.ori.identified == 0){
                            check=`
                            <a class="btn btn-sm bg-success-light mr-2"
                            @click="modal('/admin/station/check/${item.id}','是否为机构授权？')"><i class="fe fe-check"></i>
                            授权</a>
                            `
                        }else{
                            check=`
                            <a class="btn btn-sm bg-danger-light"
                            @click="modal('/admin/station/check/${item.id}','是否取消机构的授权？')"><i class="fe fe-disabled"></i>
                            取消授权</a>
                            `
                        }
                        return `<div class="actions"> ${check}</div>`
                    }   
                }]
            },
            data
        })
    }
    async check(){
        const { ctx,app }=this;
        let id = ctx.params.id

        let data = await app.model.User.findOne({
            where:{
                id:id
            }
        })

        if(data.identified==0){
            data.identified=1;
            await data.save();
            ctx.toast('授权成功','success');
        }else{
            data.identified=0;
            await data.save();
            ctx.toast('授权已取消','danger');
        }
        ctx.redirect('/admin/station')
    }
    
}

module.exports = Rescue_stationController;
